<html>
<head>
<title>地图编辑器</title>
<meta charset="utf-8" />
<link href="css/map_tool.css" rel="stylesheet" type="text/css">
<link href="css/common.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="js/jquery-1.7.js"></script>
<script type="text/javascript" src="js/common.js"></script>
<script type="text/javascript" src="js/entry_control.js"></script>
<script type="text/javascript" src="js/ArrayList.js"></script>
<script type="text/javascript" src="js/json2.js"></script>
<script type="text/javascript">
	var cur_tool = getUrlParam("tool");
	switch (cur_tool) {
	case 'mapEditor':
		//当窗口加载实例化实体控制器
		window.onload = function() {
			$(".entry_controller").remove();
			$("#toolbar").html("");
			$("#toolbar")
					.append(
							"<p class='title'>工具栏</p>"
									+ "<p>"
									+ "	<input type='radio' name='tools' value='construct_key_point'"+
					"		id='construct_key_point' checked='checked' /><label"+
					"		for='construct_key_point'>构建道路关键点</label>"
									+ "</p>"
									+ "<p>"
									+ "	<input type='radio' name='tools' value='construct_road'"+
					"		id='construct_road' /><label for='construct_road'>构建道路</label>"
									+ "</p>"
									+ "<p>"
									+ "	<input type='radio' name='tools' value='construct_other_entry'"+
					"		id='construct_other_entry' /><label for='construct_other_entry'>构建其他实体</label>"
									+ "</p>"
									+ "<p class='title' id='option_title'>更多选项</p>"
									+ "<div id='tool_options' class='options'></div>"
									+ "<p>"
									+ "	<input type='button' value='提交所有道路信息' id='submit_all' />"
									+ "</p>");
			entry_ctrl = new Entry_Controller('#entry_controller', {
				hello : 'helloKitty'
			});
			// 初始化路径（数据库已存在路径）
			entry_ctrl.query_exist_roads(HOST + "query_roads_by_area", 'area='
					+ cur_area.id);

			// 添加事件
			controller.click(function(e) {
				if (deleting) {
					deleting = false;
					return;
				}
				var loc = new Location(e);
				entry_ctrl.locations[getProperId('kp_', 0)] = loc;
				entry_ctrl.addEntry(currentOperation, loc, controller,
						entry_ctrl.locations.length - 1);
			});

			// 选择工具
			$("p [name='tools']").click(function() {
				entry_ctrl.detect_toolbar_operation($(this));
			});

			entry_ctrl.detect_toolbar_operation($(":radio:checked"));

			$("#submit_all").click(
					function() {
						if (roads.cur == -1 || roads.size() == 0)
							return;
						var roads_json = {};
						roads_json = new Array(roads.size());
						for ( var i = 0; i < roads.size(); i++) {
							if (roads.get(i).locs.size() == 0)
								return;
							roads_json[i] = roads.get(i).toJSON();
						}
						ajax_post(HOST + "upload_points_info", 'roads='
								+ JSON.stringify(roads_json), function(msg) {
							if (!msg) {
								var json = eval("(" + msg + ")");
								entry_ctrl.update_current_road_by_json(json);
							}
						});
					});
		};

		// 当窗口大小改变纠正实体位置
		window.onresize = function() {
			var kps = $("div [id^='kp']");
			for (kp_index in kps) {
				var kp = kps.eq(kp_index);
				kp
						.css({
							position : "absolute",
							'top' : (parseInt(kp.attr("y"))
									+ controller.offset().top - Math.floor(kp
									.width() / 2)),
							'left' : (parseInt(kp.attr("x"))
									+ controller.offset().left - Math.floor(kp
									.height() / 2)),
							'z-index' : 3
						});
			}
		};

		window.onbeforeunload = function() {
			return "您正在关闭窗口,请确认数据已保存";
		};
		break;
	case 'static_block':
		break;
	case 'dynamic_block':
		break;
	default:
		$.include([ 'js/triangle.js', 'js/canvas.js' ]);
		var finding = false;
		//当窗口加载实例化实体控制器
		window.onload = function() {
			$("title").html("基本选路");
			$("#entry_controller").remove();
			$("#toolbar").html("");
			$("#toolbar")
					.append(
							"<p class='title'>基本寻路:</p>"
									+ "<p id='start'>请地图上单击选择起点与终点</p>"
									+ "<p id='end'></p>"
									+ "<p><input type='button' id='find' value='开始基本寻路'/></p>"
									+ "<p id='loading'></p>");
			entry_ctrl = new Entry_Controller('.entry_controller', {
				hello : 'helloKitty'
			});
			currentOperation = entry_ctrl.OPTERATION.BASIC;
			var CanvasTool = new CanvasUtils();
			var locs;
			// 添加事件
			controller.click(function(e) {
				if (deleting) {
					deleting = false;
					return;
				}
				if (finding) {
					return;
				}
				var loc = new Location(e);
				if (entry_ctrl.locations.length == 2) {
					entry_ctrl.locations[0] = entry_ctrl.locations[1];
					entry_ctrl.locations[1] = loc;
					CanvasTool.clearCanvas();
				} else {
					entry_ctrl.locations[entry_ctrl.locations.length] = loc;
				}
				CanvasTool.drawCircle(entry_ctrl.locations);
				if (entry_ctrl.locations[0]) {
					var kp0 = $("#kp_00");
					$("#start").html(
							"起点:(" + entry_ctrl.locations[0].x + ","
									+ entry_ctrl.locations[0].y + ")");
					$("#start").css("color", "blue");
				} else {
					$("#start").html("请地图上单击选择起点");
				}
				if (entry_ctrl.locations[1]) {
					var kp1 = $("#kp_01");
					$("#end").html(
							"终点:(" + entry_ctrl.locations[1].x + ","
									+ entry_ctrl.locations[1].y + ")");
					$("#end").css("color", "red");
				} else {
					$("#end").html("请地图上单击选择终点");
				}
			});
			$("#find")
					.click(
							function() {
								CanvasTool.clearCanvas();
								CanvasTool.drawCircle(entry_ctrl.locations);
								locs = {};
								if (entry_ctrl.locations.length != 2) {
									alert(entry_ctrl.locations.length == 0 ? "请在地图上选择起点与终点"
											: "请在地图上选择终点");
									return;
								}
								finding = true;
								$("#loading")
										.html(
												"<img src='css/images/loading.gif'/>正在寻路...");
								var x = entry_ctrl.locations[0].x;
								var y = entry_ctrl.locations[0].y;
								var x1 = entry_ctrl.locations[1].x;
								var y1 = entry_ctrl.locations[1].y;
								var json = {};
								var points = [ {
									id : 0,
									x : x,
									y : y,
									area : cur_area
								}, {
									id : 0,
									x : x1,
									y : y1,
									area : cur_area
								} ];
								json = points;
								entry_ctrl.post_ajax_then_do(HOST + "basic",
										"points=" + JSON.stringify(json),
										function(json) {
											CanvasTool.clearCanvas();
											json = eval("(" + json + ")");
											$("#loading").html("");
											finding = false;
											var size = json.length;
											locs = new ArrayList();
											for ( var i = 0; i < size; i++) {
												var cur = json[i];
												var loc = new Location(cur.x,
														cur.y, cur.id);
												locs.add(loc);
												entry_ctrl.addEntry(
														currentOperation, loc,
														controller, 0);
											}
											locs.addElementAt(0, new Location(
													x, y));
											locs.addElementAt(locs.size(),
													new Location(x1, y1));
											CanvasTool = new CanvasUtils({
												points : locs.data()
											});
											CanvasTool.drawLine();
										});
							});
		}
		break;
	}
</script>
</head>
<body>
	<div class="wrapper">
		<div class="inner">
			<div id="entry_controller" class="entry_pad round4"></div>
			<canvas id="canvas" class="draw_pad round4 entry_controller"
				width="652px" height="597px"></canvas>
			<div id="toolbar" class="toolbar round4"></div>
		</div>
	</div>
	<div class="key_point" id="sample_key_point" style="display: none;"></div>
</body>
</html>